const app = getApp();
const lbbTaskService = require('../service/task-service.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNo: 1,
        pageSize: 10,
        hosList: [],
        total: '',
        isShowCity: false,
        cityList: [],
        allcityList:[],
        currentName: '',
        wordValue:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('options=>>', options)
        this.setData({
            cityId: options.cityId
        })
        this.getDetailList()
        this.getCityList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    //城市下拉项
    ondropdown() {
        this.setData({
            isShowCity: !this.data.isShowCity,
        })
    },
    //点击选择城市
    onCity(e) {
        console.log(e)
        let selectCityId = e.currentTarget.dataset.cityid
        this.setData({
            cityId: e.currentTarget.dataset.cityid,
            currentName: e.currentTarget.dataset.name,
            isShowCity: false,
            pageNo: 1,
            hosList: []
        })
        this.getDetailList()
    },
    //触底加载下一页
    ontolower(e) {
        if (this.data.pageNo * this.data.pageSize < this.data.total) {
            this.setData({
                pageNo: this.data.pageNo + 1
            })
            this.getDetailList()
        }
    },
    //关键词搜索城市
    onkeyword(e) {
        let cityList=this.data.allcityList.filter(item=>{
            if(item.name.indexOf(e.detail)!=-1){
                return item
            }
        })
        this.setData({
            cityList:cityList,
            wordValue:e.detail
        })
    },
    //获取城市列表
    getCityList() {
        let that = this
        lbbTaskService.getCityList({
            level: 1
        }, function (res) {
            let currentName = res.data.find(item => item.id == that.data.cityId)?.name
            that.setData({
                cityList: res.data,
                allcityList:JSON.parse(JSON.stringify(res.data)),
                currentName: currentName
            })
        })
    },
    // 获取详情列表
    getDetailList() {
        let that = this
        lbbTaskService.getRoundIncompleteHosInfo({
            pageNo: that.data.pageNo,
            pageSize: that.data.pageSize,
            cityId: that.data.cityId
        }, function (res) {
            if (res.result == 0) {
                let olderList = that.data.hosList
                let newList = res.data.records
                let allList = olderList.concat(newList)
                that.setData({
                    hosList: allList,
                    total: res.data.total
                })
            }
        })
    },
    //去医院详情
    toHosDetail(e) {
        console.log(e)
        let hospitalId = e.currentTarget.dataset.hospitalid
        let versionIsNew = e.currentTarget.dataset.versionIsnew
        wx.navigateTo({
            url: `/pages/hospitalDetail/hospitalDetail?hospitalId=${hospitalId}&versionIsNew=${versionIsNew}&cityId=${this.data.cityId?this.data.cityId:0}`,
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})